﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/**
 * 委托
 */
namespace P19_1 {
    internal class Program {
        static void Main(string[] args) {
            Calculator calculator = new Calculator();
            //该委托指向的是 calculator.Report 这个方法
            //Action：只支持没有返回值和参数的方法委托
            Action action = new Action(calculator.Report);
            //直接调用
            calculator.Report();
            //通过委托间接调用
            //action();//这样写也行
            action.Invoke();

            //Func：支持多参数委托，具体可以看实现类
            //这里的意思是 2个输入参数，一个返回参数
            Func<int, int, int> func1 = new Func<int, int, int>(calculator.Add);
            Func<int, int, int> func2 = new Func<int, int, int>(calculator.Sub);

            int x = 100;
            int y = 200;
            int z = 0;
            z = func1(x, y);
            Console.WriteLine(z);
            z = func2.Invoke(x, y);
            Console.WriteLine(z);
            Console.ReadLine();
        }
    }

    class Calculator {
        public void Report() {
            Console.WriteLine("I have 3 methods");
        }

        public int  Add(int a,int b) {
            int result = a + b;
            return result;
        }

        public int Sub(int a, int b) {
            int result = a - b;
            return result;
        }
    }
}
